/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MultiThread;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Oho
 */
public class Connection extends Thread{
    ServerSocket sv ;
    ObjectInputStream inp;
    ObjectOutputStream out;
    int port;
    int idplayer=0;
    boolean flag=true; //id nguoi choi
      ArrayList<ReadWrite>arrThread;
    public Connection(int port){
        try {
          
            this.port=port;
            sv=new ServerSocket(port);
            arrThread= new ArrayList<>();
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void run()
    {
        
        while(true)
        {
            try {
                Socket sk  = sv.accept();
                inp= new ObjectInputStream(sk.getInputStream());
                out = new ObjectOutputStream(sk.getOutputStream());
                JOptionPane.showMessageDialog(null, "co 1 client ket noi den");
                new ReadWrite(inp, out, arrThread,flag,idplayer).start();
                flag=false;
                idplayer++;
            } catch (IOException ex) {
                Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
}
